<?php

namespace app\api\controller;

use app\api\service\LoginService;
use app\api\validate\LoginValidate;
use think\response\Json;

class Login extends ApiController
{
    /**
     * 账号登录
     * @return Json
     */
    public function login()
    {
        $params = (new LoginValidate())->post()->checkParam('login');
        $res = LoginService::login($params);
        if($res) {
            return $this->success($res);
        } else {
            return $this->error(LoginService::getError());
        }
    }

    /**
     * 账号注册
     * @return Json
     */
    public function register()
    {
        $params = (new LoginValidate())->post()->checkParam('register');
        $res = LoginService::register($params);
        if($res) {
            return $this->success($res);
        } else {
            return $this->error(LoginService::getError());
        }
    }

    /**
     * 微信公众号跳转url
     * @return Json
     */
    public function getWxOaCodeUrl()
    {
        $params = (new LoginValidate())->post()->checkParam('url');
        $res = LoginService::getWxOaCodeUrl($params['url']);
        if($res) {
            return $this->success(['url' => $res]);
        } else {
            return $this->error(LoginService::getError());
        }
    }

    /**
     * 微信公众号登录
     * @return Json
     */
    public function wxOaLogin()
    {
        $params = (new LoginValidate())->post()->checkParam('wxLogin');
        $res = LoginService::wxOaLogin($params);
        if($res) {
            return $this->success($res);
        } else {
            return $this->error(LoginService::getError());
        }
    }

    /**
     * 获取微信扫码登录二维码
     * @return Json
     */
    public function getWxOpScanCodeUrl()
    {
        $params = (new LoginValidate())->post()->checkParam('url');
        $res = LoginService::getWxOpScanCodeUrl($params['url']);
        if($res) {
            return $this->success($res);
        } else {
            return $this->error(LoginService::getError());
        }
    }

    /**
     * 微信扫码登录
     * @return Json
     */
    public function wxScanLogin()
    {
        $params = (new LoginValidate())->post()->checkParam('wxLogin');
        $res = LoginService::wxScanLogin($params);
        if($res) {
            return $this->success($res);
        } else {
            return $this->error(LoginService::getError());
        }
    }

    /**
     * 微信小程序登录
     * @return Json
     */
    public function wxApLogin()
    {
        $params = (new LoginValidate())->post()->checkParam('wxLogin');
        $res = LoginService::wxApLogin($params);
        if($res) {
            return $this->success($res);
        } else {
            return $this->error(LoginService::getError());
        }
    }

    /**
     * 账号注册
     * @return Json
     */
    public function forgetPassword()
    {
        $params = (new LoginValidate())->post()->checkParam('forgetPassword');
        $res = LoginService::forgetPassword($params);
        if($res) {
            return $this->success($res);
        } else {
            return $this->error(LoginService::getError());
        }
    }

    /**
     * 退出登录
     * @return Json
     */
    public function logout()
    {
        (new LoginService())->logout($this->userInfo);
        return $this->success();
    }
}